package com.shiguiwu.springmybatis.spring.scope;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.concurrent.TimeUnit;

/**
 * @description: test
 * @author: stone
 * @date: Created by 2021/3/9 10:54
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.scope
 */
public class SpringScopeTests {

    public static void main(String[] args) throws InterruptedException {
        //手动创建容器
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/scope.xml");
        //启动容器
        applicationContext.refresh();
        //向容器中注册scope
        ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
        beanFactory.registerScope(ThreadScope.THREAD_SCOPE, new ThreadScope());
        //使用容器获取bean
        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                Shark shark = (Shark) applicationContext.getBean("shark");
                Shark shark1 = (Shark) applicationContext.getBean("shark");
                System.out.println("获取的bean是否一样：" + (shark == shark1));
            }).start();

            TimeUnit.SECONDS.sleep(1);

        }
    }
}
